using System.Collections.Generic;

namespace ICP_MS.Plugin.Experiment;

public class MassRegionList : List<MassRegion>
{
	public void Add(double left, double right)
	{
		Add(new MassRegion(left, right));
	}

	public MassRegionList Clone()
	{
		MassRegionList list = new MassRegionList();
		ForEach(delegate(MassRegion t)
		{
			list.Add(t);
		});
		return list;
	}

	public void RemoveRegionList(MassRegionList list)
	{
		if (list == null)
		{
			return;
		}
		foreach (MassRegion mr in list)
		{
			RemoveRegion(mr);
		}
		Sort();
	}

	public void RemoveRegion(MassRegion region)
	{
		for (int i = 0; i < base.Count; i++)
		{
			if (!(base[i].Right < region.Left) && !(base[i].Left > region.Right))
			{
				if (base[i].Left >= region.Left && base[i].Right <= region.Right)
				{
					RemoveAt(i);
					i--;
				}
				else if (base[i].Left >= region.Left && base[i].Right > region.Right)
				{
					base[i].Left = region.Right;
				}
				else if (base[i].Left < region.Left && base[i].Right <= region.Right)
				{
					base[i].Right = region.Left;
				}
				else if (base[i].Left < region.Left && base[i].Right > region.Right)
				{
					Insert(i + 1, new MassRegion(region.Right, base[i].Right));
					base[i].Right = region.Left;
				}
			}
		}
	}

	public void AddRegion(MassRegion region)
	{
		Add(region);
		AdjustRegion();
	}

	public void AdjustRegion()
	{
		Sort();
		for (int i = 0; i < base.Count - 1; i++)
		{
			MassRegion ms0 = base[i];
			MassRegion ms1 = base[i + 1];
			if (ms0.Right > ms1.Left)
			{
				ms0.Right = ((ms0.Right >= ms1.Right) ? ms0.Right : ms1.Right);
				RemoveAt(i + 1);
				i--;
			}
		}
	}
}
